package com.hnust.dao;

import com.hnust.domain.Course;
import com.hnust.domain.record.CourseRecord;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;
import java.util.List;

/**
 *创建人：张汝往、彭鑫淼
 *创建时间：2020/10/6，2020/10/5
 *描述：课程管理的DAO
 */
@Mapper
@Repository
public interface CourseDao {
    @Select("select cou_id as courseId,name as name, cou_number as code from course where col_id=#{collegeId} order by code limit #{currentPage},#{rows}")
    public List<CourseRecord> getCourse(@Param("collegeId") String collegeId, @Param("currentPage") Integer currentPage, @Param("rows") Integer rows);

    //根据学院ID获取课程总数
    @Select("select count(*) from course where col_id=#{collegeId}")
    public int getCourseNum(@Param("collegeId") String collegeId);

    //分页查询：根据学院Id、课程名返回与课程名相似的所有课程信息
    @Select("select cou_id as courseId,name as name, cou_number as code from course where col_id=#{collegeId} and name like concat('%',#{courseName},'%') order by code limit #{currentPage},#{rows}")
    public List<CourseRecord> queryCourse(@Param("collegeId") String collegeId, @Param("courseName")String courseName, @Param("currentPage") Integer currentPage,@Param("rows") Integer rows);

    //根据学院Id、课程名返回与课程名相似的所有课程的数量
    @Select("select count(*) from course where col_id=#{collegeId} and  name like concat('%',#{courseName},'%')")
    public int queryCourseCount(@Param("collegeId") String collegeId, @Param("courseName")String courseName);

    //插入课程信息
    @Insert("insert into course (cou_id, name, cou_number, col_id) value (#{couId},#{name},#{couNumber},#{colId})")
    public Boolean addCourse(Course course);

    //删除课程信息
    @Delete("delete from course where cou_id =#{courseId}")
    public Boolean deleteCourse(@Param("courseId") String courseId);

    //更新课程信息
    @Update("update course set name = #{name},cou_number=#{code} WHERE cou_id = #{courseId}")
    public Boolean modifyCourse(CourseRecord courseRecord);

    @Select("select * from course where col_id=#{collegeId}")
    @ResultType(Course.class)
    public List<Course> findCourseByCollegeId(String collegeId);

    @Select("select name from course where cou_id=#{courseID}")
    @ResultType(String.class)
    public String findCourseNameById(String courseID);

    /*
     * @Author Tonited
     * @Description //TODO 根据课程代码获取课程id
     * @Date 19:14 2020/10/29
     * @Param [num课程代码]
     * @return String课程id
     **/
    @Select("select cou_id from course where cou_number=#{num}")
    @ResultType(String.class)
    String getCouIdByCouNum(String num);

    @Insert("insert into chapter (cha_id,name,chapter,first,second,third,cou_id) value (#{cha_id},#{name},#{chapter},#{first},#{second},#{third},#{cou_id})")
    @ResultType(Integer.class)
    Integer insertChapter(String cha_id,String name,int chapter,int first, int second,int third, String cou_id);

}
